<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

/**
 * 用户操作日志管理
 */

class UserlogController extends BaseController {

	public function index(){
		$psize = 12;
		$msg = I('get.msg','','trim');
		$s_time = I('get.time','','trim');
		$s_type = I('get.type',"", 'trim');
		$keys = I('get.key',-1,'intval');

		if( !$psize )
			$psize = $this->m_psize;
		else if( $psize > 12 )
			$psize = $this->m_psize;
		$wheres = array();
		$map = array();
		$wheres['_string'] = '1=1';

		$MUserLog = M('Userlog');
		//获取我的下属部门列表
		$Sector = M('Sector');
		$Role = M('Role');

		if($msg)	$wheres['msg'] = array('like','%'.$msg.'%');
		if($s_time)	$wheres['create_time'] = array('like','%'.$s_time.'%');
		//日志类型搜索
		if($s_type && $s_type != 'all'){
			$wheres['type'] = array('eq',$s_type);
		}else if($s_type && $s_type == 'all')
			$wheres['type'] = array('EXP','IS NOT NULL');
		//查看方式切换：我的日志、下属日志
		if($keys && $keys == "1") 			//我的操作日志
			$wheres['user_id'] = array('eq',$this->m_user['user_id']);
		else if($keys && $keys == "2"){		//下属的操作日志
			$wheres['user_id'] = array('neq',$this->m_user['user_id']);//去掉用户自己的操作日志
			$map['is_deleted'] = array('neq',1);
			$array = $Sector->field("sector_id,sector_name,parent_sector") -> where($map)->select();
			$arr = list_to_sector($array,$this->m_user['sector_id']);	//获取用户部门+下属部门
			foreach ($arr as $key => $value) {
				$new_arr[] = $value['sector_id'];
			}
			$wheres['user_sector'] = array('in',$new_arr);
			$maps = array();
			$maps['parent_role'] = array('eq',$this->m_user['role_id']);
			$find_res = $Role ->field("role_id,parent_role")->where($maps)->find();
			if(empty($find_res)){		//若没有岗位下属则是普通员工，则下属操作日志列表为空
				$res['rcode'] = 2;
				$res['msg'] = "操作日志列表为空";
				e_json($res);
			}
		}
		else
			$wheres['user_id'] = array('EXP','IS NOT NULL');

		$cnt = $MUserLog->join("sector on sector.sector_id=userlog.user_sector")
						->join("role on role.role_id=userlog.user_role")
						->where( $wheres )->count();
		$Page = new \Common\ORG\Page( $cnt, $psize );
		$res = array('rcode'=>1,'data'=>NULL, 'msg'=> 'OK');
		$userlog_list = $MUserLog
								->field("userlog.user_name,userlog.user_sector,userlog.type,userlog.request_url,userlog.request_method,userlog.request_data,
									userlog.create_time,userlog.msg,s.sector_name,r.role_name")
								->join("sector as s on s.sector_id=userlog.user_sector")
								->join("role as r on r.role_id=userlog.user_role")
								->where($wheres)
								->order("userlog.create_time desc ")
					            ->limit($Page->firstRow.','.$Page->listRows )
					            ->select();
		$sql = $MUserLog -> getLastSql();
		if( $userlog_list ){
  			$res['rcode'] = 1;
  			$res['msg'] = "OK";
	        $res['sql'] = $sql;
	        $res['roles_list'] = $new_arr;
  			$res['data'] = $userlog_list;
  			$res['pall'] =  $Page->getTotalPages();
  			$res['p'] = $Page->nowPage;
  			$res['psize'] = $psize;
  			$res['where'] = $wheres;
		}else{
	       	$res['rcode'] = 2;
	       	$res['roles_list'] = $new_arr;
	       	$res['sql'] = $sql;
			$res['msg'] = "操作日志列表为空";
		}
		e_json( $res );
	}
}
